/********************************************************************************
 * Project Name		[JavaEE_Components]
 * File Name     	[CheckingMailsByImap.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package javamail.mail;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;

import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.MimeUtility;

import org.junit.Before;
import org.junit.Test;

import com.sun.mail.imap.IMAPMessage;

import javamail.utils.MailConstants;
import javamail.utils.MailUtils;
import javamail.utils.PasswordUtils;

/**
 * 使用IMAP协议查看邮件
 * 
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class CheckingMailsByImap {

	private static Properties properties = null;

	private static String emailAddress = "javamail_sns@sina.com";

	@Before
	public void init() {
		properties = new Properties();
		properties.put("mail.store.protocol", MailConstants.PROTOCAL_IMAP);
		properties.put("mail.imap.host", MailConstants.IMAP_HOST_OF_SINA);
		properties.put("mail.imap.port", MailConstants.IMAP_PORT_OF_SINA);
		properties.put("mail.imap.auth", MailConstants.IS_AUTHORIZATION_NEED);
		properties.put("mail.debug", String.valueOf(MailConstants.IS_DEBUG_MOD_ENABLED));
	}

	/**
	 * 使用IMAP协议查看邮件并发送反馈
	 * 
	 * @throws Exception
	 */
	public static void checkMailsByImap() throws Exception {
		// 创建Session对象并连接到指定的邮箱
		Session session = Session.getInstance(properties);
		Store store = session.getStore("imap");
		store.connect(emailAddress, PasswordUtils.readPassword(emailAddress));

		// 获得收件箱
		Folder folder = store.getFolder("INBOX");
		// 打开收件箱
		folder.open(Folder.READ_WRITE);

		// 获取邮件列表
		System.out.println("一共有邮件" + folder.getMessageCount() + "封");
		System.out.println("新邮件有" + folder.getNewMessageCount() + "封");
		System.out.println("未读邮件有" + folder.getUnreadMessageCount() + "封");
		System.out.println("删除邮件有" + folder.getDeletedMessageCount() + "封");

		// 获得所有邮件并解析
		Message[] messages = folder.getMessages();
		for (Message message : messages) {
			IMAPMessage imapMessage = (IMAPMessage) message;
			String subject = MimeUtility.decodeText(imapMessage.getSubject());
			boolean isSeen = MailUtils.isSeen(imapMessage);
			if (!isSeen) {
				System.out.println("[" + subject + "]未读, 现在开始读吗?");

				BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
				String answer = reader.readLine();
				if ("yes".equalsIgnoreCase(answer)) {
					// 将邮件标为已读, 并向服务器发送反馈
					message.setFlag(Flag.SEEN, true);
				}
			}
		}
		folder.close(true);
		store.close();
	}

	@Test
	public void testCheckMailsByImap() throws Exception {
		checkMailsByImap();
	}
}
